package com.atcc.demo.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.UUID;

import javax.servlet.ServletRequest;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.atcc.demo.common.PatternValidater;
import com.atcc.demo.domain.TestUser;
import com.atcc.demo.domain.UserInfo;
import com.atcc.demo.service.UserInfoService;
import com.atcc.demo.util.RedisUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/test")
public class TestController {

	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private UserInfoService userInfoService;
	
	@GetMapping("/getUser")
	public Object getUser() {
		TestUser testUser = new TestUser("张三",233);
		redisUtil.set("testUser",testUser);
		return redisUtil.get("testUser");
	}
	
	@GetMapping("/login/loginTest/{phoneNumber}")
	@ApiOperation(value="Token测试",notes="Token测试")
	public String loginTest(
			@PathVariable(value="phoneNumber") String phoneNumber) {
		if (phoneNumber != null) {
			UserInfo userInfo = userInfoService.getUserInfoByPhoneNumber(phoneNumber);
			String tokenkey = "";
			tokenkey = "usertoken_atcc_" + userInfo.getphonenumber();
			String token = UUID.randomUUID().toString().replace("-", "");
			redisUtil.set("tokenExercise", token);
			redisUtil.expire("tokenExercise", 120);
			return "登录成功,当前token为: "+token;
		} else {
			return "没有匹配的手机号";
		}
	}
	
	@GetMapping("/login")
	@ApiOperation(value="登录认证",notes="登录认证")
	public String loginTest(
			@NotNull(message = "用户名不能为空") 
			@RequestParam("userName") String userName,
			@NotNull(message = "密码不能为空")
			@RequestParam("password") String password,
			@NotNull(message = "手机号不能为空") 
			@RequestParam("phoneNumber") String phoneNumber) {
		if (phoneNumber == PatternValidater.sjh) {
			UserInfo userInfo = userInfoService.getUserInfoByPhoneNumber(phoneNumber);
			if(userName == userInfo.getName()&&password == userInfo.getpassword()) {
				String tokenkey = "";
				tokenkey = "usertoken_atcc_" + userInfo.getphonenumber();
				String token = UUID.randomUUID().toString().replace("-", "");
				redisUtil.set("tokenExercise", token);
				redisUtil.expire("tokenExercise", 120);
				return "登录成功";
			}else {
				return "用户名密码不匹配";
			}
		} else {
			return "没有匹配的手机号";
		}
	}
	
	@PostMapping("simpleTest")
	public String simpleTest(ServletRequest req) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String s = null;
			if((s = br.readLine())!= null) {
				sb.append(s);
			}
			JSONObject jsonObject = JSONObject.parseObject(sb.toString());
			String data = jsonObject.getString("data");
			String test = jsonObject.getString("test");
			System.out.println(data);
			System.out.println(test);
			return test+data;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
